package com.oa.srms.apps.dao.goods.impl;

import java.sql.SQLException;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.oa.srms.apps.constants.IDEDStates;
import com.oa.srms.apps.dao.goods.IGoodInfoDao;
import com.oa.srms.apps.entity.goods.GoodInfoValue;

public class GoodInfoDaoImpl extends HibernateDaoSupport implements IGoodInfoDao {
	@Autowired
	private List<GoodInfoValue> list;
	
	private int totalNumber;
	
	@Override
	public Integer addGoodInfo(GoodInfoValue goodInfoValue) throws Exception{
		if(goodInfoValue != null){
			this.getHibernateTemplate().save(goodInfoValue);
		}
		return goodInfoValue.getId();
	}

	@Override
	public void editGoodInfo(GoodInfoValue goodInfoValue) throws Exception{
		if(goodInfoValue != null){
			this.getHibernateTemplate().update(goodInfoValue);
		}
		
	}

	@Override
	public GoodInfoValue queryById(Integer id) throws Exception{
		return (GoodInfoValue) this.getHibernateTemplate().get(GoodInfoValue.class, id);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<GoodInfoValue> queryGoodInfo(final GoodInfoValue goodInfoValue,
			final Integer rows, final Integer pageNo) throws Exception {
		String hqlStr = "SELECT giv FROM GoodInfoValue giv WHERE 1=1";
		if (goodInfoValue.getGoodName() != null) {
			hqlStr += " AND giv.goodName = ? ";
		}
		if (goodInfoValue.getStates() != null) {
			hqlStr += " AND giv.states = ? ";
		}
		hqlStr += " AND giv.states <> ?";
		final String hql = hqlStr;
		
		list = this.getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(hql);
				int index = -1;
				if (goodInfoValue.getGoodName() != null) {
					query.setString(++index, goodInfoValue.getGoodName());
				}
				if (goodInfoValue.getStates()  != null) {
					query.setInteger(++index, goodInfoValue.getStates());
				}
				query.setInteger(++index, IDEDStates.DELETE);
				query.setFirstResult(rows * (pageNo - 1));
				query.setMaxResults(rows);
				list = query.list();
				return list;
			}
		});
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Integer getTotalPageNumber(final GoodInfoValue goodInfoValue, Integer rows) {
		int totalPageNumber = 0;
		String hqlStr = "SELECT giv FROM GoodInfoValue giv WHERE 1=1";
		if (goodInfoValue.getGoodName()!= null) {
			hqlStr += " AND giv.goodName = ?";
		}
		if (goodInfoValue.getStates() != null) {
			hqlStr += " AND giv.states = ? ";
		}
		hqlStr += " AND giv.states <> ?";
		final String hql = hqlStr;
		
		list = this.getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(hql);
				int index = -1;
				if (goodInfoValue.getGoodName()!= null) {
					query.setString(++index, goodInfoValue.getGoodName());
				}
				if (goodInfoValue.getStates()  != null) {
					query.setInteger(++index, goodInfoValue.getStates());
				}
				query.setInteger(++index, IDEDStates.DELETE);
				list = query.list();
				return list;
			}
		});
		totalNumber = list.size();
		if (totalNumber <= rows) {
			totalPageNumber = 1;
		} else {
			if (totalNumber % rows == 0) {
				totalPageNumber = totalNumber / rows;
			} else {
				totalPageNumber = totalNumber / rows + 1;
			}
		}
		return totalPageNumber;
	}

	@Override
	public Integer getTotalNumber() {
		return totalNumber;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<GoodInfoValue> queryGoodInfo(final Integer states) throws Exception {
		String hqlStr = "SELECT giv FROM GoodInfoValue giv WHERE 1=1";
		if (states != null) {
			hqlStr += " AND giv.states = ? ";
		}
		hqlStr += " AND giv.states <> ?";
		final String hql = hqlStr;
		
		list = this.getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery(hql);
				int index = -1;
				if (states  != null) {
					query.setInteger(++index, states);
				}
				query.setInteger(++index, IDEDStates.DELETE);
				list = query.list();
				return list;
			}
		});
		return list;
	}
	
	

}
